import requests
from common.mylogger import logger

class Myrequests:
    def __init__(self):
        self.hearders = {"content-type": "application/json;charset=utf-8"}

    def send_requests(self, url, method, data=None, token=None):
        self.__dear_header(token)
        logger.info("请求URL:{}".format(url))
        logger.info("请求方法:{}".format(method))
        logger.info("请求数据:{}".format(data))
        if method.upper() == "GET":
            resp = requests.request(method, url, headers=self.hearders)
        else:
            resp = requests.request(method, url, json=data, headers=self.hearders)
        logger.info("响应结果为:{}".format(resp.text))
        return resp

    def __dear_header(self, token=None):
        if token:
            self.hearders["token"] = token
        logger.info("请求头为：{}".format(self.hearders))



if __name__ == '__main__':

    mr = Myrequests()
    url = "https://yunyan-test.frp.xxbu.cn/api/sasc-aggregation-property-web/login"
    req_data = {"username": "18180573632",
                "password": "12345678",
                "userType": 0,
                "loginType": 0}
    method = "post"
    resp = mr.send_requests(url, method, req_data)
    print(resp.json())
    json_res = resp.json()
    token = json_res["data"]["token"]


    url1 = "https://yunyan-test.frp.xxbu.cn/api/sasc-aggregation-property-web/enterprise/info"
    method = "GET"
    resp1 = mr.send_requests(url1, method, token=token)
    print(resp1.json())


    url = "https://yunyan-test.frp.xxbu.cn/api/sasc-aggregation-property-web/enterprise/update"
    req_data = {"id": 164307508400001,
                 "name": "2022125测试",
                 "email": "125@qq.com",
                 "address": "4id都ianb1打16628837181",
                 "contact": "阿阳可",
                 "mobile": "13759763920",
                 "region": ["140000", "140100", "140105"],
                 "expireTime": 4102329599,
                 "gmtCreate": 1643075084,
                 "maxCommunityAmount": 33,
                 "enabled": True,
                 "webName": "asdf",
                 "webLogo": "adfgafdg",
                 "channel": 1}
    method = "post"
    resp = mr.send_requests(url, method, req_data, token=token)
    print(resp.json())

